package com.sunday.fangdaifu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.fangdaifu.R;
import com.sunday.fangdaifu.activity.task.TaskDetailActivity;
import com.sunday.fangdaifu.model.Task;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/7/3.
 */

public class TaskListAdapter extends RecyclerView.Adapter {


    private List<Task> dataSet = new ArrayList<>();
    private Context mContext;
    private int type;


    public TaskListAdapter(Context context, List<Task> datas, int type) {
        this.mContext = context;
        this.dataSet = datas;
        this.type = type;
    }

    public void setType(int type){
        this.type=type;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_task_item, null);
        RecyclerView.LayoutParams params=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        return new TaskHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Task item = dataSet.get(position);
        TaskHolder taskHolder = (TaskHolder) holder;
        taskHolder.houseName.setText(item.getHouseName());
        taskHolder.taskClass.setText(item.getLevel());
        taskHolder.time.setText(item.getStart()+"至"+item.getEnd());
        switch (item.getLevel()) {
            case "A级":
                taskHolder.taskClass.setBackgroundResource(R.drawable.border_66e04a);
                taskHolder.taskClass.setTextColor(mContext.getResources().getColor(R.color._66e04a));
                break;
            case "B级":
                taskHolder.taskClass.setBackgroundResource(R.drawable.border_ffa04c);
                taskHolder.taskClass.setTextColor(mContext.getResources().getColor(R.color._ffa04c));
                break;
            case "C级":
                taskHolder.taskClass.setBackgroundResource(R.drawable.border_ff8519);
                taskHolder.taskClass.setTextColor(mContext.getResources().getColor(R.color._ff8519));
                break;
            case "D级":
                taskHolder.taskClass.setBackgroundResource(R.drawable.border_ff230a);
                taskHolder.taskClass.setTextColor(mContext.getResources().getColor(R.color._ff230a));
                break;
            default:
                taskHolder.taskClass.setBackgroundResource(R.drawable.border_66e04a);
                taskHolder.taskClass.setTextColor(mContext.getResources().getColor(R.color._66e04a));
        }
        if (type != 3) {
            taskHolder.btn.setVisibility(item.getWriteStatus() == 1 ? View.VISIBLE : View.GONE);
        } else {
            taskHolder.btn.setVisibility(View.GONE);
        }
        taskHolder.btn.setTag(item);
        taskHolder.item.setTag(item);
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    class TaskHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        @Bind(R.id.house_name)
        TextView houseName;
        @Bind(R.id.task_class)
        TextView taskClass;
        @Bind(R.id.btn)
        TextView btn;
        @Bind(R.id.item)
        RelativeLayout item;
        @Bind(R.id.time)
        TextView time;

        public TaskHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            item.setOnClickListener(this);
            btn.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Task item = (Task) v.getTag();
            switch (v.getId()) {
                case R.id.btn:
                    Intent intent1 = new Intent(mContext, TaskDetailActivity.class);
                    intent1.putExtra("id", item.getId());
                    mContext.startActivity(intent1);
                    break;
                case R.id.item:
                    if (type == 3) {
                        Intent intent = new Intent(mContext, TaskDetailActivity.class);
                        intent.putExtra("id", item.getId());
                        mContext.startActivity(intent);
                    }
                    break;
            }
        }
    }
}
